package com.itheima.pinda.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.pinda.DTO.DriverJobDTO;
import com.itheima.pinda.common.utils.PageResponse;
import com.itheima.pinda.entity.DriverJob;
import com.itheima.pinda.service.IDriverJobService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * 司机作业单
 * </p>
 * 调度过程中会根据运输任务计算产生司机作业单 [计算待实现], 司机根据作业单进行运输作业
 */
@Slf4j
@Validated
@Api(tags = "司机作业单管理")
@RestController
@RequestMapping("/driver-job")
public class DriverJobController {
    @Resource
    private IDriverJobService driverJobService;

    /**
     * 新增司机作业单
     *
     * @param dto 司机作业单信息
     * @return 司机作业单信息
     */
    @PostMapping("")
    @ApiOperation("新增司机作业单")
    public DriverJobDTO save(@RequestBody DriverJobDTO dto) {
        DriverJob driverJob = new DriverJob();
        BeanUtils.copyProperties(dto, driverJob);
        driverJobService.saveDriverJob(driverJob);
        DriverJobDTO result = new DriverJobDTO();
        BeanUtils.copyProperties(driverJob, result);
        return result;
    }

    /**
     * 修改司机作业单信息
     *
     * @param id  司机作业单id
     * @param dto 司机作业单信息
     * @return 司机作业单信息
     */
    @PutMapping("/{id}")
    @ApiOperation("修改司机作业单信息")
    public DriverJobDTO updateById(@PathVariable("id") String id, @Valid @RequestBody DriverJobDTO dto) {
        dto.setId(id);
        DriverJob driverJob = new DriverJob();
        BeanUtils.copyProperties(dto, driverJob);
        driverJobService.updateById(driverJob);
        return dto;
    }

    /**
     * 获取司机作业单分页数据
     *
     * @param dto 查询参数
     * @return 司机作业单分页数据
     */
    @PostMapping("/page")
    @ApiOperation("获取司机作业单分页数据")
    public PageResponse<DriverJobDTO> findByPage(@Valid @RequestBody DriverJobDTO dto) {
        Integer page = 1;
        Integer pageSize = 10;
        List<DriverJobDTO> dtoList = new ArrayList<>();
        long total = 0L;
        long pages = 0L;
        if (dto != null) {
            if (dto.getPage() != null) {
                page = dto.getPage();
            }
            if (dto.getPageSize() != null) {
                pageSize = dto.getPageSize();
            }
            IPage<DriverJob> driverJobIPage = driverJobService.findByPage(page, pageSize, dto.getId(), dto.getDriverId(), dto.getStatus(), dto.getTaskTransportId());
            driverJobIPage.getRecords().forEach(driverJob -> {
                DriverJobDTO resultDto = new DriverJobDTO();
                BeanUtils.copyProperties(driverJob, resultDto);
                dtoList.add(resultDto);
            });
            total = driverJobIPage.getTotal();
            pages = driverJobIPage.getPages();
        }
        return PageResponse.<DriverJobDTO>builder().items(dtoList).pagesize(pageSize).page(page).counts(total)
                .pages(pages).build();
    }

    /**
     * 根据id获取司机作业单信息
     *
     * @param id 司机作业单id
     * @return 司机作业单信息
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id获取司机作业单信息")
    public DriverJobDTO findById(@PathVariable("id") String id) {
        DriverJobDTO dto = new DriverJobDTO();
        DriverJob driverJob = driverJobService.getById(id);
        if (driverJob != null) {
            BeanUtils.copyProperties(driverJob, dto);
        } else {
            dto = null;
        }
        return dto;
    }


    /**
     * 获取所有司机作业单信息
     *
     * @param dto 司机作业单dto
     * @return 司机作业单列表
     */
    @PostMapping("/findAll")
    @ApiOperation("获取所有司机作业单信息")
    public List<DriverJobDTO> findAll(@Valid @RequestBody DriverJobDTO dto) {
        List<DriverJobDTO> driverJobDTOS = new ArrayList<>();
        List<DriverJob> driverJobs = driverJobService.findAll(null, dto.getId(), dto.getDriverId(),
                dto.getStatus(), dto.getTaskTransportId());
        if (driverJobs != null) {
            for (DriverJob driverJob : driverJobs) {
                dto = new DriverJobDTO();
                BeanUtils.copyProperties(driverJob, dto);
                driverJobDTOS.add(dto);
            }
        }
        return driverJobDTOS;
    }
}
